
import java.net.*;
import java.io.*;

public class ChatClientThread extends Thread
{
	int port;
	
	public ChatClientThread(int p)
	{
		super();
		port = p;
		System.out.println("ChatClientThread created");
	}
    public void run()
    {
    	InetAddress group;
    	/*try
    	{
    		group = InetAddress.getByName("239.1.2.3");
    	}
    	catch(IOException ioe)
    	{
			System.out.println("IOException in ChatClientThread.run:");
			ioe.printStackTrace();
    	}*/
        MulticastSocket socket;
        
        try
        {
            group = InetAddress.getByName("224.0.0.1");
			System.out.println("Creating socket");
            socket = new MulticastSocket(port);
            socket.joinGroup(group);
            byte[] buf = new byte[100];
            DatagramPacket dp = new DatagramPacket(buf, buf.length);
			System.out.println("Listening for messages");
            while(Connection.getInstance().isConnected())
            {
                socket.receive(dp);
                String message = new String(dp.getData());
                ClientController.getInstance(ClientFrame.getInstance()).printToChat(message, Connection.getInstance().getTime());
            }
        }
        catch(UnknownHostException uhe)
        {
            System.out.println("UHException in ChatClientThread.run:");
			uhe.printStackTrace();
        }
        catch(IOException ioe)
        {
            System.out.println("IOException in ChatClientThread.run:");
			ioe.printStackTrace();
        }
    }
}
        
        
        
